package com.leyantech.ecp.agenx.session.manager.di;

import com.leyantech.chaos.kafka.KafkaConsumerStringClient;
import com.leyantech.chaos.kafka.KafkaProducerStringClient;
import com.leyantech.ecp.agenx.common.di.RedissonModule;
import com.leyantech.ecp.agenx.common.handler.ActionHandler;
import com.leyantech.ecp.agenx.common.message.EnumWsFnType;
import com.leyantech.ecp.agenx.common.message.MessageHelper;
import com.leyantech.ecp.agenx.session.manager.handler.ConfigHandler;
import com.leyantech.ecp.agenx.session.manager.consumer.AssistantEventConsumer;
import com.leyantech.grpc.clients.LeyanGrpcClientOverrideModule;

import com.google.common.collect.Lists;
import com.google.inject.AbstractModule;
import com.google.inject.Injector;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import io.vertx.core.json.JsonObject;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.reflections.Reflections;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author eahau, {@literal <eahau@foxmail.com>}
 * @date 2022-11-17.
 */
public class SessionConsumerModule extends AbstractModule {

  @Override
  protected void configure() {

    install(LeyanGrpcClientOverrideModule.buildBySystemPropPlatform());
    install(new RedissonModule());
  }

  @Singleton
  @Provides
  public KafkaProducerStringClient provideKafkaProducerStringClient() {
    return KafkaProducerStringClient.newBuilder("session-manager").build();
  }

  @Singleton
  @Provides
  public KafkaConsumerStringClient provideKafkaConsumerStringClient(
      AssistantEventConsumer assistantEventConsumer) {
    return KafkaConsumerStringClient.newBuilder("session-manager")
        .setProcessor(assistantEventConsumer)
        .setWarmupRecord(getWarmupRecords())
        .build();
  }

  @Singleton
  @Provides
  public List<ActionHandler> provideActionHandlers(Injector injector) {
    return new Reflections("com.leyantech.ecp.agenx.session.manager.handler")
        .getSubTypesOf(ActionHandler.class)
        .stream()
        .map(injector::getInstance)
        .collect(Collectors.toList());
  }

  @Singleton
  @Provides
  public List<ConfigHandler> provideConfigHandlers(Injector injector) {
    return new Reflections("com.leyantech.ecp.agenx.session.manager.handler")
        .getSubTypesOf(ConfigHandler.class)
        .stream()
        .map(injector::getInstance)
        .collect(Collectors.toList());
  }

  List<ConsumerRecord<String, String>> getWarmupRecords() {

    final JsonObject body = new JsonObject(
        "{\"store_id\":\"warmup\",\"assistant_id\":\"warmup\",\"assistant_name\":\"warmup\",\"client_version\":\"warmup\",\"client_uuid\":\"warmup\"}");

    final JsonObject logoutMessage = MessageHelper
        .buildMessage(EnumWsFnType.ASSISTANT_LOGOUT, body);

    final JsonObject loginMessage = MessageHelper.buildMessage(EnumWsFnType.ASSISTANT_LOGIN, body);

    final JsonObject configChangeMessage = MessageHelper
        .buildMessage(EnumWsFnType.AGENT_BUSINESS_CONFIG_MSG, body);

    final String key = "warmup.warmup";

    return Lists.newArrayList(
        new ConsumerRecord<>("", 0, 0, key, loginMessage.toString()),
        new ConsumerRecord<>("", 0, 0, key, configChangeMessage.toString()),
        new ConsumerRecord<>("", 0, 0, key, logoutMessage.toString())
    );
  }

}
